package com.buybuyyi.stylistcrm.user.dao.impl;

import com.buybuyyi.stylistcrm.consts.DateFormatPattern;
import com.buybuyyi.stylistcrm.user.dao.RequisitionDao;
import com.buybuyyi.stylistcrm.user.mapper.UserRequisitionMapper;
import com.buybuyyi.stylistcrm.user.po.UserRequisitionWithBLOBs;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import stylistcrm.core.query.OrderObject;
import stylistcrm.core.query.PaginationConf;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.user.EnumUserType;
import stylistcrm.core.services.user.profile.Gender;
import stylistcrm.core.services.user.requisition.QueryRequisitionConditionDto;
import stylistcrm.core.services.user.requisition.Requisition;
import stylistcrm.core.services.user.requisition.EnumRequisitionState;
import stylistcrm.core.services.user.requisition.RequisitionDto;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yanwc on 2016/10/9.
 */
@Repository
public class RequisitionDaoImpl implements RequisitionDao {

    @Autowired
    UserRequisitionMapper userRequisitionMapper;

    /**
     * 分页查询
     * @param queryCondition
     * @return
     * @throws BizException
     */
    @Override
    public List<RequisitionDto> queryByPage(QueryRequisitionConditionDto queryCondition) throws BizException {
        List<UserRequisitionWithBLOBs> userRequisitionList=userRequisitionMapper.queryByPage(queryCondition);
        return convert2RequisitionDto(userRequisitionList);
    }

    /**
     * 查询总的数量
     * @param queryCondition
     * @return
     * @throws BizException
     */
    @Override
    public Integer queryTotalCount(QueryRequisitionConditionDto queryCondition) throws BizException {
        Integer totalCount=userRequisitionMapper.queryTotalCount(queryCondition);
        return totalCount;
    }

    /**
     * 保存用户申请
     * @param requisitionDto
     * @return
     * @throws BizException
     */
    @Override
    public boolean insert(RequisitionDto requisitionDto) throws BizException {
        if(null==requisitionDto){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        UserRequisitionWithBLOBs requisitionWithBLOBs=new UserRequisitionWithBLOBs();
        requisitionWithBLOBs.setSecUserId(requisitionDto.getSecUserId());
        requisitionWithBLOBs.setNickName(requisitionDto.getNickName());
        requisitionWithBLOBs.setRealName(requisitionDto.getRealName());
        requisitionWithBLOBs.setCellPhone(requisitionDto.getCellPhone());
        requisitionWithBLOBs.setHeadImageUrl(requisitionDto.getHeadImageUrl());
        if(StringUtil.isBlank(requisitionDto.getUserType())){
            requisitionWithBLOBs.setUserType(EnumUserType.DESIGNER.getCode());
        }else{
            requisitionWithBLOBs.setUserType(requisitionDto.getUserType());
        }
        if(null==requisitionDto.getState()){
            requisitionWithBLOBs.setState(EnumRequisitionState.WaittingCheck.getValue());
        }else{
            requisitionWithBLOBs.setState(requisitionDto.getState());
        }
        if(null==requisitionDto.getGender()){
            requisitionWithBLOBs.setGender(Gender.MALE.getCode());
        }else{
            requisitionWithBLOBs.setGender(requisitionDto.getGender());
        }
        requisitionWithBLOBs.setBankAccountNumber(requisitionDto.getBankAccountNumber());
        if(StringUtil.isNotBlank(requisitionDto.getBirthDay())){
            requisitionWithBLOBs.setBirthDay(DateUtil.parseDate(requisitionDto.getBirthDay(), DateFormatPattern.PATTERN_YYYY_MM_DD));
        }
        requisitionWithBLOBs.setBrandIntroduction(requisitionDto.getBrandIntroduction());
        requisitionWithBLOBs.setBusinessAddress(requisitionDto.getBusinessAddress());
        requisitionWithBLOBs.setBusinessLicense(requisitionDto.getBusinessLicense());
        requisitionWithBLOBs.setExperience(requisitionDto.getExperience());
        requisitionWithBLOBs.setResume(requisitionDto.getResume());
        requisitionWithBLOBs.setIsPartTime("true".equals(requisitionDto.getIsPartTime())?"1":"0");
        requisitionWithBLOBs.setCreateTime(new Date());
        Integer ret=userRequisitionMapper.insertSelective(requisitionWithBLOBs);
        if(ret>0){
            return true;
        }
        return false;
    }

    /**
     * 更新申请审核状态
     * @param pkId 编号
     * @param state 状态
     * @return
     */
    @Override
    public boolean updateRequisitionState(Long pkId, EnumRequisitionState state) {

        return userRequisitionMapper.updateRequisitionState(pkId,state);
    }

    /**
     * 通过用户id获取用户申请单
     * @param secUserId
     * @return
     */
    @Override
    public List<RequisitionDto> queryByUserId(Long secUserId) {
        List<UserRequisitionWithBLOBs> userRequisitionList=userRequisitionMapper.queryByUserId(secUserId);

        return convert2RequisitionDto(userRequisitionList);
    }

    /**
     *
     * @param secUserId
     * @param state
     * @return
     */
    @Override
    public List<RequisitionDto> queryByUserId(Long secUserId, EnumRequisitionState state) {
        List<UserRequisitionWithBLOBs> userRequisitionList=userRequisitionMapper.queryByUserIdAndState(secUserId,state);

        return convert2RequisitionDto(userRequisitionList);
    }

    /**
     * 通过主键查询
     * @param id
     * @return
     */
    @Override
    public RequisitionDto queryById(Long id) {
        List<UserRequisitionWithBLOBs> userRequisitionList=userRequisitionMapper.queryByPkId(id);
        if(null!=userRequisitionList&&!userRequisitionList.isEmpty()){
            return convert2RequisitionDto(userRequisitionList.get(0));
        }
        return null;
    }

    /**
     * 请求集合转换
     * @param userRequisitionList
     * @return
     */
    private List<RequisitionDto> convert2RequisitionDto(List<UserRequisitionWithBLOBs> userRequisitionList){
        List<RequisitionDto> requisitionDtoList=new ArrayList<>();
        if(null!=userRequisitionList&&!userRequisitionList.isEmpty()){
            for(UserRequisitionWithBLOBs userRequisition:userRequisitionList){
                RequisitionDto requisitionDto=convert2RequisitionDto(userRequisition);
                if(null!=requisitionDto){
                    requisitionDtoList.add(requisitionDto);
                }
            }
        }
        return requisitionDtoList;
    }

    /**
     * 请求转换dto
     * @param userRequisition
     * @return
     */
    private RequisitionDto convert2RequisitionDto(UserRequisitionWithBLOBs userRequisition){
        if(null==userRequisition){
            return null;
        }
        RequisitionDto requisitionDto=new RequisitionDto();
        requisitionDto.setUserName(userRequisition.getUserName());
        requisitionDto.setLoginName(userRequisition.getLoginName());
        requisitionDto.setSecUserId(userRequisition.getSecUserId());
        requisitionDto.setState(userRequisition.getState());
        requisitionDto.setStateName(EnumRequisitionState.getEnum(userRequisition.getState()).getName());
        requisitionDto.setUserType(userRequisition.getUserType());
        requisitionDto.setUserTypeName(EnumUserType.getEnum(userRequisition.getUserType()).getDesc());
        requisitionDto.setHeadImageUrl(userRequisition.getHeadImageUrl());
        requisitionDto.setCellPhone(userRequisition.getCellPhone());
        requisitionDto.setBankAccountNumber(userRequisition.getBankAccountNumber());
        if(null!=userRequisition.getBirthDay()){
            requisitionDto.setBirthDay(DateUtil.formatDate(userRequisition.getBirthDay(),DateFormatPattern.PATTERN_YYYY_MM_DD));
        }
        requisitionDto.setGender(userRequisition.getGender());
        requisitionDto.setSex(Gender.getEnum(userRequisition.getGender()).getDesc());
        requisitionDto.setNickName(userRequisition.getNickName());
        requisitionDto.setRealName(userRequisition.getRealName());
        requisitionDto.setId(userRequisition.getId());
        requisitionDto.setBrandIntroduction(userRequisition.getBrandIntroduction());
        requisitionDto.setBusinessAddress(userRequisition.getBusinessAddress());
        requisitionDto.setBusinessLicense(userRequisition.getBusinessLicense());
        requisitionDto.setExperience(userRequisition.getExperience());
        requisitionDto.setResume(userRequisition.getResume());
        if(null!=userRequisition.getCreateTime()){
            requisitionDto.setCreateTime(DateUtil.formatDate(userRequisition.getCreateTime(),DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
        }
        requisitionDto.setIsPartTime(userRequisition.getIsPartTime());
        requisitionDto.setIsPartTimeName("1".equals(userRequisition.getIsPartTime())?"是":"否");
        return requisitionDto;
    }

}
